package com.axend.aerosense.common.util;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentTransaction;

import com.axend.aerosense.common.R;
import com.axend.aerosense.common.base.IHostLayout;

public class FragmentUtil {

//
//    public static void setFragmentInAnim(){
//
//    }
//
//    public static void setFragmentOutAnim(){
//
//    }

    /** 进入页面动画 */
    public static void setFragmentInAnim(FragmentTransaction transaction){
        transaction.setCustomAnimations(
                R.anim.common_in_from_right,
                R.anim.common_out_to_left);
    }

    /** 返回页面动画 */
    public static void setFragmentOutAnim(FragmentTransaction transaction){
        transaction.setCustomAnimations(
                R.anim.common_in_from_left,
                R.anim.common_out_to_right);
    }
    public static void add(FragmentActivity activity, IHostLayout hostLayout, Fragment fragment, Fragment hidFragment, boolean addToBackStack){
        add(activity, hostLayout.getContentId(), fragment, hidFragment, addToBackStack);
    }

    public static void add(FragmentActivity activity, int contentId, Fragment fragment, Fragment hidFragment, boolean addToBackStack){
        FragmentTransaction fragmentTransaction = activity.getSupportFragmentManager().beginTransaction();
        if(null != hidFragment){
            fragmentTransaction.hide(hidFragment);
        }
        fragmentTransaction.add(contentId, fragment, fragment.getClass().getSimpleName());
        fragmentTransaction.setCustomAnimations(
                com.axend.aerosense.common.R.anim.common_in_from_left,
                com.axend.aerosense.common.R.anim.common_out_to_right,
                com.axend.aerosense.common.R.anim.common_in_from_left,
                com.axend.aerosense.common.R.anim.common_out_to_right
        );
        if(addToBackStack){
            fragmentTransaction.addToBackStack(fragment.getClass().getSimpleName());
        }
        fragmentTransaction.commitAllowingStateLoss();
    }

    public static void replace(FragmentActivity activity, IHostLayout hostLayout, Fragment fragment, Fragment hidFragment, boolean addToBackStack){
        replace(activity, hostLayout.getContentId(), fragment, hidFragment, addToBackStack);
    }

    public static void replace(FragmentActivity activity, int contentId, Fragment fragment, Fragment hidFragment, boolean addToBackStack){
        FragmentTransaction fragmentTransaction = activity.getSupportFragmentManager().beginTransaction();
        if(null != hidFragment){
            fragmentTransaction.hide(hidFragment);
        }
        fragmentTransaction.replace(contentId, fragment, fragment.getClass().getSimpleName());
        fragmentTransaction.setCustomAnimations(
                com.axend.aerosense.common.R.anim.common_in_from_left,
                com.axend.aerosense.common.R.anim.common_out_to_right,
                com.axend.aerosense.common.R.anim.common_in_from_left,
                com.axend.aerosense.common.R.anim.common_out_to_right
        );
        if(addToBackStack){
            fragmentTransaction.addToBackStack(fragment.getClass().getSimpleName());
        }
        fragmentTransaction.commitAllowingStateLoss();
    }
}
